<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zookeeper Watch 机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: linear-gradient(135deg, #1e1e2e 0%, #2d2d44 100%);
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: rgba(255,255,255,0.05);
            padding: 8px 16px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        pre code {
            color: #e4e4e7;
            font-family: 'Consolas', 'Monaco', monospace;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
            margin: 3rem 0;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative overflow-hidden bg-gradient-to-br from-purple-900 via-indigo-900 to-blue-900 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-7xl font-bold mb-6 serif-font">
                    Zookeeper <span class="gradient-text bg-gradient-to-r from-yellow-400 to-orange-400">Watch</span> 机制
                </h1>
                <p class="text-xl md:text-2xl mb-8 text-gray-200">
                    深入理解分布式系统中的事件通知核心
                </p>
                <div class="flex justify-center space-x-4">
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-clock mr-2"></i> 实时监控
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-sync mr-2"></i> 事件驱动
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-shield-alt mr-2"></i> 强一致性
                    </span>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0 h-20 bg-gradient-to-t from-gray-50 to-transparent"></div>
    </section>

    <!-- Introduction -->
    <section class="container mx-auto px-6 py-16 max-w-5xl">
        <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap serif-font">Z</span>ookeeper 中的 Watch 机制是其核心特性之一，用于实现分布式系统中的事件通知。它允许客户端对 Zookeeper 中的节点进行监控，以便在节点的状态或数据发生变化时获得通知。这种机制为分布式应用提供了高效、可靠的协调能力，是构建分布式系统的重要基石。
            </p>
        </div>
    </section>

    <!-- 实现原理 -->
    <section class="container mx-auto px-6 py-16 max-w-5xl">
        <h2 class="text-4xl font-bold mb-12 text-center serif-font text-gray-800">
            <i class="fas fa-cogs text-purple-600 mr-3"></i>实现原理
        </h2>
        
        <div class="grid md:grid-cols-2 gap-8">
            <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-4">
                    <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-plus-circle text-purple-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">1. 注册 Watch</h3>
                </div>
                <p class="text-gray-600 leading-relaxed">
                    客户端通过 API 请求在 Zookeeper 的节点上注册 Watch。Watch 是一种一次性事件通知机制，用于监听节点的变化，例如节点的创建、删除或数据更新。
                </p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-4">
                    <div class="w-12 h-12 bg-indigo-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-bolt text-indigo-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">2. 事件触发</h3>
                </div>
                <p class="text-gray-600 leading-relaxed">
                    当节点的数据或状态发生变化时，Zookeeper 会触发已注册的 Watch 事件。Zookeeper 将事件信息发送到所有注册了 Watch 的客户端。
                </p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-4">
                    <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-tasks text-blue-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">3. 事件处理</h3>
                </div>
                <p class="text-gray-600 leading-relaxed">
                    客户端接收到事件通知后，通过 Watcher 处理相应的事件。处理完成后，客户端需要重新注册 Watch，以继续接收后续的事件通知。
                </p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-4">
                    <div class="w-12 h-12 bg-green-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-recycle text-green-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">4. Watch 生命周期</h3>
                </div>
                <p class="text-gray-600 leading-relaxed">
                    Watch 是一次性的。一旦触发，Watch 将被自动移除。客户端需要重新注册 Watch 以继续监控节点的变化。
                </p>
            </div>
        </div>

        <!-- Code Example -->
        <div class="mt-12 code-block">
            <div class="code-header flex items-center justify-between">
                <span class="text-gray-300 text-sm font-medium">Java 示例代码</span>
                <i class="fas fa-code text-gray-400"></i>
            </div>
            <pre class="p-6 overflow-x-auto"><code>zk.getData("/node", new Watcher() {
    @Override
    public void process(WatchedEvent event) {
        // 处理事件
    }
}, null);</code></pre>
        </div>
    </section>

    <!-- Watch 机制流程图 -->
    <section class="container mx-auto px-6 py-16 max-w-5xl">
        <h2 class="text-4xl font-bold mb-12 text-center serif-font text-gray-800">
            <i class="fas fa-project-diagram text-purple-600 mr-3"></i>Watch 机制流程
        </h2>
        <div class="bg-white rounded-2xl shadow-xl p-8">
            <div class="mermaid">
                graph TD
                    A[客户端] -->|1. 注册Watch| B[Zookeeper Server]
                    B -->|2. 确认注册| A
                    C[其他客户端] -->|3. 修改节点| B
                    B -->|4. 触发Watch事件| A
                    A -->|5. 处理事件| D[事件处理器]
                    D -->|6. 重新注册Watch| B
                    
                    style A fill:#667eea,stroke:#4c51bf,stroke-width:2px,color:#fff
                    style B fill:#48bb78,stroke:#38a169,stroke-width:2px,color:#fff
                    style C fill:#ed8936,stroke:#dd6b20,stroke-width:2px,color:#fff
                    style D fill:#9f7aea,stroke:#805ad5,stroke-width:2px,color:#fff
            </div>
        </div>
    </section>

    <div class="section-divider"></div>

    <!-- 优势与劣势 -->
    <section class="container mx-auto px-6 py-16 max-w-5xl">
        <div class="grid md:grid-cols-2 gap-12">
            <!-- 优势 -->
            <div>
                <h2 class="text-3xl font-bold mb-8 text-gray-800 serif-font">
                    <i class="fas fa-thumbs-up text-green-600 mr-3"></i>优势
                </h2>
                <div class="space-y-6">
                    <div class="bg-gradient-to-r from-green-50 to-emerald-50 rounded-xl p-6 border-l-4 border-green-500">
                        <h4 class="font-bold text-lg mb-2 text-gray-800">高效的事件通知</h4>
                        <p class="text-gray-600">Zookeeper 的 Watch 机制使得客户端能够实时感知节点的变化，而不是频繁地轮询。这样可以减少网络带宽和服务器负载。</p>
                    </div>
                    <div class="bg-gradient-to-r from-green-50 to-emerald-50 rounded-xl p-6 border-l-4 border-green-500">
                        <h4 class="font-bold text-lg mb-2 text-gray-800">简化的异步编程</h4>
                        <p class="text-gray-600">使用 Watch 机制可以简化分布式系统中的异步事件处理。客户端只需注册 Watch，无需主动检查节点状态，从而简化了代码逻辑。</p>
                    </div>
                    <div class="bg-gradient-to-r from-green-50 to-emerald-50 rounded-xl p-6 border-l-4 border-green-500">
                        <h4 class="font-bold text-lg mb-2 text-gray-800">即时响应</h4>
                        <p class="text-gray-600">客户端可以立即响应节点的变化，例如在分布式锁、配置管理等场景中，能够迅速做出决策。</p>
                    </div>
                    <div class="bg-gradient-to-r from-green-50 to-emerald-50 rounded-xl p-6 border-l-4 border-green-500">
                        <h4 class="font-bold text-lg mb-2 text-gray-800">内建的强一致性保证</h4>
                        <p class="text-gray-600">Zookeeper 的 Watch 机制与其强一致性保证紧密结合，确保事件通知的准确性和一致性。</p>
                    </div>
                </div>
            </div>

            <!-- 劣势 -->
            <div>
                <h2 class="text-3xl font-bold mb-8 text-gray-800 serif-font">
                    <i class="fas fa-exclamation-triangle text-red-600 mr-3"></i>劣势
                </h2>
                <div class="space-y-6">
                    <div class="bg-gradient-to-r from-red-50 to-pink-50 rounded-xl p-6 border-l-4 border-red-500">
                        <h4 class="font-bold text-lg mb-2 text-gray-800">一次性